Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / Streams-Patterns-Events / A Practical Guide / PG_06d_Parallel_Patterns.html
blob39c9d93bf8c048f29cf473c78211d8d562269ba4
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.48">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
20 span.s1 {color: #0000bf}
21 span.s2 {color: #0000bf}
22 span.s3 {text-decoration: underline ; color: #0000bf}
23 span.s4 {color: #0000bf}
24 span.s5 {color: #000000}
25 span.s6 {color: #0000bf}
26 span.s7 {color: #007300}
27 span.s8 {font: 12.0px Helvetica}
28 span.Apple-tab-span {white-space:pre}
29 </style>
30 </head>
31 <body>
32 <p class="p1"><b>Parallelizing event patterns</b></p>
33 <p class="p2"><br></p>
34 <p class="p3">There are a couple of different ways to have several patterns playing at the same time. The most obvious is to play them separately. The patterns' events get scheduled independently on their clock(s) and run concurrently. None of these patterns need to have any knowledge of the others. One advantage of this approach is that the patterns can be stopped and started independently.</p>
35 <p class="p2"><br></p>
36 <p class="p3">The other is to combine them into a parallel stream. The result is a single pattern object that can be played or stopped only as one unit. Some degree of interactive control is lost, but there are times when merging several patterns is necessary -- for instance, converting a pattern into a Score object for NRT rendering.</p>
37 <p class="p2"><br></p>
38 <ul>
39 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ppar(list, repeats):</b> Start each of the event patterns in the 'list' at the same time. When the last one finishes, the <a href="../Patterns/Ppar.html"><span class="s1">Ppar</span></a> also stops. If repeats &gt; 1, all the subpatterns start over again from the beginning.</li>
40 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ptpar(list, repeats):</b> Here, the list consists of [timeOffset0, pattern0, timeOffset1, pattern1...]. Each pattern starts after the number of beats given as its time offset. The patterns can start at different times relative to each other.</li>
41 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgpar(list, repeats):</b> Like Ppar, but it creates a separate group for each subpattern.</li>
42 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgtpar(list, repeats):</b> This is like Ptpar with separate groups for the subpatterns.</li>
43 </ul>
44 <p class="p2"><br></p>
45 <p class="p3">An excellent example of <a href="../Patterns/Ppar.html"><span class="s2">Ppar</span></a> and <a href="../Patterns/Pseq.html"><span class="s2">Pseq</span></a> used together to structure an entire piece (Kraftwerk's "Spacelab") can be found in <a href="../../../examples/pieces/spacelab.scd"><span class="s3">examples/pieces/spacelab.scd</span></a>.</p>
46 <p class="p2"><br></p>
47 <p class="p4"><b>Dynamic parallelizing</b></p>
48 <p class="p2"><br></p>
49 <p class="p3">Ppar and its cousins are good for a fixed set of parallel patterns -- that is, you need to know in advance how many patterns will be parallelized. Once the parallel pattern starts, there is no way to add more streams to it. To keep adding streams, use <a href="../Patterns/Pspawner.html"><span class="s4">Pspawner</span></a> and <a href="../Patterns/Pspawn.html"><span class="s4">Pspawn</span></a>. For the purpose of this overview, some basic features will be illustrated in a couple of simple examples. These classes have more capabilities; refer to their help files for specifics.</p>
50 <p class="p2"><br></p>
51 <ul>
52 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pspawner(routineFunc):</b> The function is run in a Routine. A Spawner object gets passed into this Routine, and this object is used to add or remove streams to/from the parallel stream. New patterns can be added in sequence or in parallel.</li>
53 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pspawn(pattern, spawnProtoEvent):</b> Supports most of the features of Pspawner, but uses a pattern to control the Spawner object instead of a Routine function.</li>
54 </ul>
55 <p class="p2"><br></p>
56 <p class="p3">This example uses <a href="../Patterns/Pspawner.html"><span class="s1">Pspawner</span></a> to trigger overlapping scale segments at different speeds. Unlike Ppar, which could handle a fixed number before stopping, Pspawner can keep going indefinitely.</p>
57 <p class="p2"><br></p>
58 <p class="p5">(</p>
59 <p class="p6"><span class="s5">p = </span><span class="s6">Pspawner</span><span class="s5">({ </span><span class="s6">|sp|</span><span class="s5"><span class="Apple-tab-span"> </span></span>// sp = the Spawner object</p>
60 <p class="p5"><span class="Apple-tab-span"> </span>loop {</p>
61 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// run a new pattern in parallel</p>
62 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// the pattern is finite</p>
63 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// after a few events, it stops and the Pspawner forgets about it</p>
64 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>sp.par(<span class="s6">Pbind</span>(</p>
65 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\degree</span>, <span class="s6">Pseries</span>(rrand(-5, 7), #[-1, 1].choose, rrand(4, 7)),</p>
66 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\pan</span>, rrand(-1.0, 1.0),</p>
67 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s7">\dur</span><span class="s5">, rrand(0.1, 0.3)<span class="Apple-tab-span"> </span></span>// duration is chosen once for each pattern</p>
68 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>));</p>
69 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// tell the Spawner to wait a bit before the next pattern goes</p>
70 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// DO NOT use numBeats.wait for this!</p>
71 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Everything must go through the Spawner</p>
72 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>sp.wait(rrand(1, 4) * 0.25);</p>
73 <p class="p5"><span class="Apple-tab-span"> </span>}</p>
74 <p class="p5">}).play;</p>
75 <p class="p5">)</p>
76 <p class="p7"><br></p>
77 <p class="p5">p.stop;</p>
78 <p class="p2"><br></p>
79 <p class="p3">The same, written using <a href="../Patterns/Pspawn.html"><span class="s1">Pspawn</span></a>:</p>
80 <p class="p2"><br></p>
81 <p class="p5">(</p>
82 <p class="p8"><span class="s5">p = </span>Pspawn<span class="s5">(</span>Pbind<span class="s5">(</span></p>
83 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span></span><span class="s7">\method</span><span class="s5">, </span><span class="s7">\par</span><span class="s5">,<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// embed patterns in parallel</p>
84 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// generate the subpattern in a Pfunc (so there's a new pattern each time)</p>
85 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// Pfunc returns the pattern without rendering the stream</p>
86 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>// -- important for Pspawn</p>
87 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// See the Pspawn helpfile for other ways to embed patterns</p>
88 <p class="p9"><span class="s5"><span class="Apple-tab-span"> </span></span>\pattern<span class="s5">, </span><span class="s6">Pfunc</span><span class="s5"> {</span></p>
89 <p class="p8"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>Pbind<span class="s5">(</span></p>
90 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\degree</span>, <span class="s6">Pseries</span>(rrand(-5, 7), #[-1, 1].choose, rrand(4, 7)),</p>
91 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\pan</span>, rrand(-1.0, 1.0),</p>
92 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s7">\dur</span><span class="s5">, rrand(0.1, 0.3)<span class="Apple-tab-span"> </span></span>// duration is chosen once for each pattern</p>
93 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
94 <p class="p5"><span class="Apple-tab-span"> </span>},</p>
95 <p class="p6"><span class="s5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// The \delta key is used automatically for the spawner.wait() call</p>
96 <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">\delta</span>, <span class="s6">Pwhite</span>(1, 4, <span class="s6">inf</span>) * 0.25</p>
97 <p class="p5">)).play;</p>
98 <p class="p5">)</p>
99 <p class="p7"><br></p>
100 <p class="p5">p.stop;</p>
101 <p class="p2"><br></p>
102 <p class="p2"><br></p>
103 <p class="p10"><span class="s5">Previous:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_06c_Composition_of_Patterns.html"><span class="s8">PG_06c_Composition_of_Patterns</span></a></span></p>
104 <p class="p10"><span class="s5">Next:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_06e_Language_Control.html"><span class="s8">PG_06e_Language_Control</span></a></span></p>
105 </body>
106 </html>